package com.zhouheng.springbootredis.module.girl.service.impl;

import com.zhouheng.springbootredis.common.constants.RedisKeyPrefix;
import com.zhouheng.springbootredis.common.entity.Girl;
import com.zhouheng.springbootredis.common.entity.GirlInsertVo;
import com.zhouheng.springbootredis.common.exception.BaseException;
import com.zhouheng.springbootredis.common.repository.GirlRepository;
import com.zhouheng.springbootredis.module.girl.service.GirlService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * 测试redis
 *
 * @author 周恒
 * @date 20190226 15:35:34
 * @since v1.0
 */
@Service
@Log4j2
public class GirlServiceImpl implements GirlService {

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Autowired
    private GirlRepository girlRepository;

    @Override
    public Girl findOne(Integer id) {
        String key = RedisKeyPrefix.GIRL + id;
        // 缓存存在
        boolean hasKey = redisTemplate.hasKey(key);
        if (hasKey) {
            // 从缓存中取
            Girl girl = (Girl)redisTemplate.opsForValue().get(key);
            log.info("从缓存中获取了Girl！");
            return girl;
        }
        // 从数据库取，并存回缓存
        Girl girl = girlRepository.findOne(id);
        log.info("从数据库中获取Girl！");
        if(null != girl){
            // 放入缓存，并设置缓存时间
            redisTemplate.opsForValue().set(key, girl, 600, TimeUnit.SECONDS);
            log.info("将Girl存入了缓存");
        }else{
            log.error("未查询到Girl信息，id={}", id);
        }
        return girl;
    }

    @Override
    public void updateGirl(Girl girl) {
        Integer girlId = girl.getId();
        Girl findGirl = girlRepository.findOne(girlId);
        if(null == findGirl){
            throw new BaseException("更新的girl，id不存在" + girlId);
        }
        //一定要返回，更新的某些字段，数据库还有些其他字段未更新的，也需要和缓存同步
        if(null != girl.getName()){
            findGirl.setName(girl.getName());
        }
        if(null != girl.getAge()){
            findGirl.setAge(girl.getAge());
        }
        findGirl = girlRepository.save(findGirl);
        String key = RedisKeyPrefix.GIRL + girlId;
        boolean hasKey = redisTemplate.hasKey(key);
        // 缓存存在，更新缓存；不存在不用管
        if (hasKey) {
            redisTemplate.delete(key);
            log.info("更新Girl时候，从缓存中删除Girl >> " + girlId);
            redisTemplate.opsForValue().set(key, findGirl, 600, TimeUnit.SECONDS);
            log.info("从缓存中删除Girl后，再重新存入Girl >> " + girlId);
        }
    }

    @Override
    public void deleteById(Integer id) {
        Girl findGirl = girlRepository.findOne(id);
        if(null == findGirl){
            throw new BaseException("删除的girl，id不存在" + id);
        }
        girlRepository.delete(id);
        String key = RedisKeyPrefix.GIRL + id;
        boolean hasKey = redisTemplate.hasKey(key);
        // 缓存存在，删除缓存
        if (hasKey) {
            redisTemplate.delete(key);
            log.info("删除Girl时候，从缓存中删除Girl >> " + id);
        }
    }

    @Override
    public void insert(GirlInsertVo girl) {
        Girl save = new Girl();
        save.setAge(girl.getAge());
        save.setName(girl.getName());
        girlRepository.save(save);
    }

}
